<?php
/**
 * Created by PhpStorm.
 * User: PGF(592472116@qq.com)
 * Date: 2016/8/22
 * Time: 10:48
 */

namespace Appapi\Controller;
use Common\Controller\HomebaseController;


class NotifyController  extends HomebaseController
{

    public function  index(){
        file_put_contents('./post.txt',$_POST);
        $config_pri=M('config_private')->where('id=1')->find();
        $status=$_POST['status'];
        $customerid=$_POST['customerid'];
        $order_no=$_POST['sdorderno'];
        $total_fee=$_POST['total_fee'];
        $paytype=$_POST['paytype'];
        $sdpayno=$_POST['sdpayno'];
        $remark=$_POST['remark'];
        $sign=$_POST['sign'];
        $mysign=md5('customerid='.$config_pri['wx_mchid'].'&status='.$status.'&sdpayno='.$sdpayno.'&sdorderno='.$order_no.'&total_fee='.$total_fee.'&paytype='.$paytype.'&'.$config_pri['wx_key']);
        if($sign==$mysign){
            if($status=='1') {
                $map['status'] = 0;
                $map['money'] = $total_fee;
                $map['orderno'] = $order_no;
                $order = M('users_charge')->where($map)->find();
                if ($order) {
                    $data['status'] = 1;
                    $data['orderno'] = $order_no;
                    //$data['trade_no'] = $trade_no;
                    $data['addtime'] = time();
                    $res = M('users_charge')->where('id=' . $order['id'])->save($data);
                    $coin = $order['coin'];
                    if ($order['coin_give'] > 0) {
                        $coin = $coin + $order['coin_give'];
                    }
                    if ($order['effect_type'] == 2) {
                        $month = $order['month'];
                        $efftime = strtotime('+' . $month . 'month');
                        $where['member_validity'] = $efftime;
                        $where['bonus_time'] = time();
                        $user_flag = M('users')->where('id=' . $order['uid'])->save($where);
                    } else {
                        $user_flag = M('users')->where('id=' . $order['uid'])->setInc('coin', $coin);
                    }
                }
              }
         }
        echo 'success';
    }

	//--支付宝异步通知地址
	public function aliPay(){

        require_once("./wxpay/lib/WxPay.Api.php");
        require_once("./wxpay/lib/RequestHandler.class.php");
        require_once("./wxpay/lib/ClientResponseHandler.class.php");
        require_once("./wxpay/lib/PayHttpClient.class.php");
        //require_once("./alipay_app/class/Utils.class.php");
        $this->resHandler = new \ClientResponseHandler();
        $this->reqHandler = new \RequestHandler();
        $this->pay = new \PayHttpClient();
        $xml = file_get_contents('php://input');
        $wx_key=M('config_private')->where('id=1')->getField('aliapp_key_android');
        file_put_contents('3.txt',$xml);//检测是否执行callback方法，如果执行，会生成1.txt文件，且文件中的内容就是通知参数
        $this->resHandler->setContent($xml);
        $this->resHandler->setKey($wx_key);
        if($this->resHandler->isTenpaySign()){

            if($this->resHandler->getParameter('status') == 0 && $this->resHandler->getParameter('result_code') == 0){
                //echo $this->resHandler->getParameter('status');
                // 11;
                //校验单号和金额是否一致，更改订单状态等业务处理
                $out_trade_no=$this->resHandler->getParameter('out_trade_no');
                $total_fee=$this->resHandler->getParameter('total_fee');
                $trade_no=$this->resHandler->getParameter('out_transaction_id');
                //\Utils::dataRecodes('接口回调收到通知参数',$this->resHandler->getAllParameters());
                //echo 'success';
                file_put_contents('4.txt',$out_trade_no);//如果生成2.txt,说明前一步的输出success是有执行
                $order_no=$out_trade_no;
                $total_fee=$total_fee/100;
                //file_put_contents('./wx.txt',json_encode($_POST));
                /* if($_POST['seller_id'] != '2088721119028384'){
                     exit;
                 }*/
                $map['status'] =0;
                $map['money'] = $total_fee;
                $map['orderno'] = $order_no;
                $order = M('users_charge')->where($map)->find();
                if($order){
                    $data['status'] =1;
                    $data['orderno']=$order_no;
                    $data['trade_no'] = $trade_no;
                    $data['addtime']  = time();
                    $res = M('users_charge')->where('id='.$order['id'])->save($data);
                    $coin=$order['coin'];
                    if($order['coin_give']>0){
                        $coin = $coin+$order['coin_give'];
                    }
                    if($order['effect_type'] ==2){
                        $month=$order['month'];
                        $efftime =strtotime('+'.$month.'month');
                        $where['member_validity']=$efftime;
                        $where['bonus_time']=time();
                        $user_flag = M('users')->where('id=' . $order['uid'])->save($where);
                    }else {
                        $user_flag = M('users')->where('id=' . $order['uid'])->setInc('coin', $coin);
                    }

                }
                echo 'success';
                exit();
            }else{
                echo 'failure1';
                exit();
            }
        }else{
            echo 'failure2';
        }

    }

    //微信支付回调地址
    public function WXPay()
    {
        $xmldata = file_get_contents("php://input");
        file_put_contents('./xmldata.txt', $xmldata);
        libxml_disable_entity_loader(true);
        $data = (array)simplexml_load_string($xmldata, 'SimpleXMLElement', LIBXML_NOCDATA);
        file_put_contents('./wx.txt', json_encode($data));
        if ($data) {
            $out_trade_no = $data['out_trade_no'];//订单号
            $money = $data['total_fee'] / 100;  //充值金额
            $map['status'] =0;
            $map['money'] = $money;
            $map['orderno'] = $out_trade_no;
            $order = M('users_charge')->where($map)->find();
            if($order){
                $data['status'] =1;
                $data['orderno']=$out_trade_no;
               // $data['trade_no'] = $trade_no;
                $data['addtime']  = time();
                $res = M('users_charge')->where('id='.$order['id'])->save($data);
                $coin=$order['coin'];
                if($order['coin_give']>0){
                    $coin = $coin+$order['coin_give'];
                }
                if($order['effect_type'] ==2){
                    $month=$order['month'];
                    $efftime =strtotime('+'.$month.'month');
                    $where['member_validity']=$efftime;
                    $where['bonus_time']=time();
                    $user_flag = M('users')->where('id=' . $order['uid'])->save($where);
                }else {
                    $user_flag = M('users')->where('id=' . $order['uid'])->setInc('coin', $coin);
                }

                //添加三级分销
                $dis = M('distribution')->find();
                if($dis['status'] == 1) { //是否开启三级分销
                    $price = $order['money'];//实际购买播豆支付的钱
                    $id= $order['id'];
                    $member=M('Member','jzb_');
                    $ucenter_member=M('Ucenter_member','jzb_');

                    $mobile  =M('users')->where('id='.$order['uid'])->getField('user_login');
                    $uid =$ucenter_member->where('username='.$mobile)->getField('id');
                    //一级分销
                    $referrer1 = $member->where('uid=' .$uid)->getField('referrer');
                    if ($referrer1) {
                        $rer1_uid = $ucenter_member->where('username=' . $referrer1)->getField('uid');
                        $one_money = $price * $dis['one_ratio']/100;
                        $re1 = M('member')->where('uid=' . $rer1_uid)->setField('amount', $one_money);
                        payrecord($rer1_uid, $id, $one_money, 5); //佣金记录
                        //二级分销
                        $referrer2 = $member->where('uid=' .$rer1_uid)->getField('referrer');
                        if($referrer2){
                            $rer2_uid = $ucenter_member->where('username=' . $referrer2)->getField('uid');
                            $two_money = $price*$dis['two_ratio']/100;
                            $re2 = $member->where('uid=' . $rer2_uid)->setField('amount', $two_money);
                            payrecord($rer2_uid, $id, $two_money, 5); //佣金记录

                            //三级分销
                            $referrer3 = $member->where('uid=' .$rer2_uid)->getField('referrer');
                            if($referrer3) {
                                $rer3_uid =$ucenter_member->where('username=' . $referrer2)->getField('uid');
                                $three_money = $price * $dis['three_ratio'] / 100;
                                $re2 =$member->where('uid=' . $rer3_uid)->setField('amount', $three_money);
                                payrecord($rer3_uid, $id, $three_money, 5); //佣金记录
                            }  //三级分销if结束
                        }   //二级分销if结束
                    }  //一级分销if结束
                }
                echo 'SUCCESS';
            }

            $data['return_code'] = 'SUCCESS';
            $data['return_msg'] = 'OK';
            $xml = data_to_xml2( $data );
            echo $xml;
            die();
        }

    }


    /**
     * 后台异步通知处理
     */
    public function callback(){
        require_once("./wxpay/lib/WxPay.Api.php");
        require_once("./wxpay/lib/RequestHandler.class.php");
        require_once("./wxpay/lib/ClientResponseHandler.class.php");
        require_once("./wxpay/lib/PayHttpClient.class.php");
        //require_once("./alipay_app/class/Utils.class.php");
        $this->resHandler = new \ClientResponseHandler();
        $this->reqHandler = new \RequestHandler();
        $this->pay = new \PayHttpClient();
        $xml = file_get_contents('php://input');
        $wx_key=M('config_private')->where('id=1')->getField('wx_key');
        file_put_contents('1.txt',$xml);//检测是否执行callback方法，如果执行，会生成1.txt文件，且文件中的内容就是通知参数
        $this->resHandler->setContent($xml);
        $this->resHandler->setKey($wx_key);
        if($this->resHandler->isTenpaySign()){

            if($this->resHandler->getParameter('status') == 0 && $this->resHandler->getParameter('result_code') == 0){
                //echo $this->resHandler->getParameter('status');
                // 11;
                //校验单号和金额是否一致，更改订单状态等业务处理
                $out_trade_no=$this->resHandler->getParameter('out_trade_no');
                $total_fee=$this->resHandler->getParameter('total_fee');
                $trade_no=$this->resHandler->getParameter('out_transaction_id');
                //\Utils::dataRecodes('接口回调收到通知参数',$this->resHandler->getAllParameters());
                //echo 'success';
                file_put_contents('2.txt',$out_trade_no);//如果生成2.txt,说明前一步的输出success是有执行
                $order_no=$out_trade_no;
                $total_fee=$total_fee/100;
                //file_put_contents('./wx.txt',json_encode($_POST));
                /* if($_POST['seller_id'] != '2088721119028384'){
                     exit;
                 }*/
                $map['status'] =0;
                $map['money'] = $total_fee;
                $map['orderno'] = $order_no;
                $order = M('users_charge')->where($map)->find();
                if($order){
                    $data['status'] =1;
                    $data['orderno']=$order_no;
                    $data['trade_no'] = $trade_no;
                    $data['addtime']  = time();
                    $res = M('users_charge')->where('id='.$order['id'])->save($data);
                    $coin=$order['coin'];
                    if($order['coin_give']>0){
                        $coin = $coin+$order['coin_give'];
                    }
                    if($order['effect_type'] ==2){
                        $month=$order['month'];
                        $efftime =strtotime('+'.$month.'month');
                        $where['member_validity']=$efftime;
                        $where['bonus_time']=time();
                        $user_flag = M('users')->where('id=' . $order['uid'])->save($where);
                    }else {
                        $user_flag = M('users')->where('id=' . $order['uid'])->setInc('coin', $coin);
                    }
                    //添加三级分销
                    $dis = M('distribution')->find();
                    if($dis['status'] == 1) { //是否开启三级分销
                        $price = $order['money'];//实际购买播豆支付的钱
                        $id= $order['id'];
                        $member=M('Member','jzb_');
                        $ucenter_member=M('Ucenter_member','jzb_');

                        $mobile  =M('users')->where('id='.$order['uid'])->getField('user_login');
                        $uid =$ucenter_member->where('username='.$mobile)->getField('id');
                        //一级分销
                        $referrer1 = $member->where('uid=' .$uid)->getField('referrer');
                        if ($referrer1) {
                            $rer1_uid = $ucenter_member->where('username=' . $referrer1)->getField('uid');
                            $one_money = $price * $dis['one_ratio']/100;
                            $re1 = M('member')->where('uid=' . $rer1_uid)->setField('amount', $one_money);
                            payrecord($rer1_uid, $id, $one_money, 5); //佣金记录
                            //二级分销
                            $referrer2 = $member->where('uid=' .$rer1_uid)->getField('referrer');
                            if($referrer2){
                                $rer2_uid = $ucenter_member->where('username=' . $referrer2)->getField('uid');
                                $two_money = $price*$dis['two_ratio']/100;
                                $re2 = $member->where('uid=' . $rer2_uid)->setField('amount', $two_money);
                                payrecord($rer2_uid, $id, $two_money, 5); //佣金记录

                                //三级分销
                                $referrer3 = $member->where('uid=' .$rer2_uid)->getField('referrer');
                                if($referrer3) {
                                    $rer3_uid =$ucenter_member->where('username=' . $referrer2)->getField('uid');
                                    $three_money = $price * $dis['three_ratio'] / 100;
                                    $re2 =$member->where('uid=' . $rer3_uid)->setField('amount', $three_money);
                                    payrecord($rer3_uid, $id, $three_money, 5); //佣金记录
                                }  //三级分销if结束
                            }   //二级分销if结束
                        }  //一级分销if结束
                    }
                }
                echo 'success';
                exit();
            }else{
                echo 'failure1';
                exit();
            }
        }else{
            echo 'failure2';
        }
    }




}